package response

import (
	"blog/internal/common"
	"github.com/gin-gonic/gin"
	"net/http"
)

type Response struct {
	Code    int         `json:"code"`
	Message string      `json:"message"`
	Data    interface{} `json:"data"`
}

func NewResponse(code int, message string, data interface{}) *Response {
	return &Response{Code: code, Message: message, Data: data}
}

func writeResponse(c *gin.Context, code int, message string, data interface{}) {
	c.JSON(http.StatusOK, NewResponse(code, message, data))
}

// Success 返回成功消息
func Success(ctx *gin.Context, message string) {
	writeResponse(ctx, common.COMMON_SUCCESS, message, nil)
}

// Fail 返回错误消息
func Fail(ctx *gin.Context, code int, message string) {
	writeResponse(ctx, code, message, nil)
}

// CommonFail 返回错误码-1的,通用错误消息
func CommonFail(ctx *gin.Context, message string) {
	writeResponse(ctx, common.COMMON_ERROR, message, nil)
}

// Data 返回数据
func Data(ctx *gin.Context, data interface{}) {
	writeResponse(ctx, common.COMMON_SUCCESS, "success", data)
}
